package com.shtel.bss.saas.tenant.service.impl;

import com.github.pagehelper.PageInfo;
import com.shtel.bss.saas.core.data.common.service.DataServiceImpl;
import com.shtel.bss.saas.tenant.bo.SysGlobalAuthBo;
import com.shtel.bss.saas.tenant.bo.SysTenantBo;
import com.shtel.bss.saas.tenant.dto.SysGlobalAuthDTO;
import com.shtel.bss.saas.tenant.entity.SysTenant;
import com.shtel.bss.saas.tenant.entity.SysUser;
import com.shtel.bss.saas.tenant.manager.SysTenantManager;
import com.shtel.bss.saas.tenant.manager.SysUserManager;
import com.shtel.bss.saas.tenant.mapstrut.EntityDtoMapper;
import com.shtel.bss.saas.tenant.service.ISysTenantService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * 租户表服务
 * @author xiezhiwei 2020-02-05 22:02
 */
@Service("SysTenantService")
public class SysTenantServiceImpl extends DataServiceImpl implements ISysTenantService {

    //是否被禁用(1:禁用;0:启用)
    public static final byte PROHIBIT = 1;

    @Override
    public PageInfo<SysTenantBo> querySysTenantList(String tenantName, String email, int pageSize, int pageNum) {
        SysTenant sysTenant = new SysTenant();
        sysTenant.setEmail(email);
        sysTenant.setTenantName(tenantName);
        return EntityDtoMapper.INSTANCE.toSysTenantBoPageInfo(SysTenantManager.selectEntity(sysTenant,pageSize,pageNum));
    }

    @Override
    public SysTenantBo querySysTenantByTenantId(Long tenantId) {
//        if(tenantId == null){
//            throw ExceptionBuilder.BusiException(BaseExceptionCode.PARAMETER_IS_NULL, "queryCatalog", "parentCatalogId");
//        }
        return EntityDtoMapper.INSTANCE.toSysTenantBo(SysTenantManager.selectEntityByTenantId(tenantId));

    }

    @Override
    @Transactional
    public void saveSysTenant(SysTenantBo sysTenantBo) {
        sysTenantBo.setCreateTime(new Date());
        SysTenant sysTenant = EntityDtoMapper.INSTANCE.toSysTenant(sysTenantBo);
        SysTenantManager.saveEntity(sysTenant);
        // 保存租户的同时要保存用户
        SysUser sysUser = new SysUser();
        sysUser.setTenantId(sysTenant.getTenantId());
        sysUser.setAccount(sysTenantBo.getAccount());
        sysUser.setPassword(sysTenantBo.getPassword());
        sysUser.setUserName(sysTenantBo.getTenantName());
        sysUser.setMobile(sysTenantBo.getMobile());
        sysUser.setEmail(sysTenantBo.getEmail());
        sysTenant.setCreateTime(sysTenantBo.getCreateTime());
        SysUserManager.saveEntity(sysUser);
    }

    @Override
    @Transactional
    public void updateSysTenant(SysTenantBo sysTenantBo) {
        // 保存租户的同时要保存用户
        SysTenantManager.updateEntity(EntityDtoMapper.INSTANCE.toSysTenant(sysTenantBo));
        SysUser sysUser = EntityDtoMapper.INSTANCE.toSysUser(sysTenantBo);
        SysUserManager.updateEntity(sysUser);
    }

    @Override
    @Transactional
    public void removeSysTenant(List<Long> tenantIds) {
        if(tenantIds != null && !tenantIds.isEmpty()){
            for (Long tenantId : tenantIds) {
                SysTenant sysTenant = new SysTenant();
                sysTenant.setTenantId(tenantId);
                sysTenant.setIsDelete((byte) 1);
                SysTenantManager.updateEntity(sysTenant);
                //删除租户同时删除对应的用户
                SysUser sysUser = new SysUser();
                sysUser.setTenantId(tenantId);
                PageInfo<SysUser> sysUserPageInfo = SysUserManager.selectEntity(sysUser, 1,1);
                if(CollectionUtils.isEmpty(sysUserPageInfo.getList())){
                    continue;
                }
                sysUser = sysUserPageInfo.getList().get(0);
                sysUser.setIsDelete((byte) 1);
                SysUserManager.updateEntity(sysUser);
            }
        }

    }

    @Override
    public void prohibitSysTenant(Long tenantId) {
        SysTenant sysTenantData = SysTenantManager.selectEntityByTenantId(tenantId);
        if(sysTenantData == null){
            return;
        }
        SysTenant sysTenant = new SysTenant();
        if(sysTenantData.getIsProhibit() == (byte) 1){
            sysTenant.setTenantId(tenantId);
            sysTenant.setIsProhibit((byte) 0);
        }else{
            sysTenant.setTenantId(tenantId);
            sysTenant.setIsProhibit(PROHIBIT);
        }
        SysTenantManager.updateEntity(sysTenant);
    }
}